home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dos6mm.zip / D2H.ASM < prev    next >
Assembly Source File  |  1993-03-31  |  8KB  |  193 lines

  1. ;****************************************************************************
  2. ; D2H displays a decimal number in hexadecimal format. Its syntax is:
  3. ;
  4. ;       D2H decimal-value
  5. ;
  6. ; where "decimal-value" is a decimal value from 0 to 4,294,967,295.
  7. ;****************************************************************************
  8.  
  9. code            segment
  10.                 assume  cs:code,ds:code
  11.                 org     100h
  12. begin:          jmp     main
  13.  
  14. helpmsg         db      "Displays a decimal number in hexadecimal format."
  15.                 db      13,10,13,10
  16.                 db      "D2H decimal-value",13,10,"$"
  17.  
  18. errmsg1         db      "Syntax: H2D decimal-value",13,10,"$"
  19. errmsg2         db      "Invalid digit",13,10,"$"
  20. errmsg3         db      "Number too large (cannot exceed 4,294,967,295)"
  21. crlf            db      13,10,"$"
  22.  
  23. ten             dw      10                      ;Base 10 multiplier
  24. sixteen         dw      16                      ;Base 16 divisor
  25. loword          dw      0                       ;Accumulator (low word)
  26. hiword          dw      0                       ;Accumulator (high word)
  27.  
  28. ;****************************************************************************
  29. ; Procedure MAIN
  30. ;****************************************************************************
  31.  
  32. main            proc    near
  33.                 cld                             ;Clear direction flag
  34.                 mov     si,81h                  ;Point SI to the command line
  35.                 call    scanhelp                ;Scan for a "/?" switch
  36.                 jnc     main1                   ;Branch if not found
  37.  
  38.                 mov     ah,09h                  ;Display help text
  39.                 mov     dx,offset helpmsg
  40.                 int     21h
  41.                 jmp     exit                    ;Exit
  42.  
  43. main1:          call    findchar                ;Find the start of the string
  44.                 jc      error1                  ;Error if there is no string
  45.  
  46. main2:          lodsb                           ;Get the next digit
  47.                 cmp     al,","                  ;Ignore commas
  48.                 je      main2
  49.                 cmp     al,20h                  ;Exit if it's a space, tab,
  50.                 je      main4                   ;or end-of-line character
  51.                 cmp     al,09h
  52.                 je      main4
  53.                 cmp     al,0Dh
  54.                 je      main4
  55.  
  56.                 cmp     al,"0"                  ;Error if digit is invalid
  57.                 jb      error2
  58.                 cmp     al,"9"
  59.                 ja      error2
  60.  
  61.                 xor     ah,ah                   ;Byte to word in AX
  62.                 sub     al,"0"                  ;Convert ASCII to binary
  63.                 push    ax                      ;Save result for later
  64.  
  65.                 mov     ax,loword               ;Add the digit to what
  66.                 mul     ten                     ;we've already accumulated
  67.                 push    dx
  68.                 mov     bx,ax                   ;Low word
  69.                 mov     ax,hiword
  70.                 mul     ten
  71.                 jo      error3
  72.                 pop     dx
  73.                 add     ax,dx
  74.                 jc      error4
  75.                 mov     cx,ax                   ;High word
  76.                 pop     ax
  77.                 add     bx,ax
  78.                 adc     cx,0
  79.                 jc      error5
  80.                 mov     loword,bx
  81.                 mov     hiword,cx
  82.                 jmp     main2
  83.  
  84. main4:          mov     bx,loword               ;Display the number in
  85.                 mov     ax,hiword               ;hex format
  86.                 call    bin2hex
  87.  
  88. exit:           mov     ax,4C00h                ;Return to DOS
  89.                 int     21h
  90. ;
  91. ; Error handling routines.
  92. ;
  93. error1:         mov     dx,offset errmsg1
  94.                 jmp     short error
  95. error2:         mov     dx,offset errmsg2
  96.                 jmp     short error
  97. error3:         add     sp,2
  98. error4:         add     sp,2
  99. error5:         mov     dx,offset errmsg3
  100. error:          mov     ah,09h
  101.                 int     21h
  102.                 jmp     exit
  103. main            endp
  104.  
  105. ;****************************************************************************
  106. ; BIN2HEX displays the number in AX:BX in hexadecimal format.
  107. ;****************************************************************************
  108.  
  109. bin2hex         proc    near
  110.                 xor     cx,cx
  111.  
  112. b2h1:           xor     dx,dx
  113.                 div     sixteen
  114.                 mov     si,ax
  115.                 mov     ax,bx
  116.                 div     sixteen
  117.                 mov     bx,ax
  118.                 mov     ax,si
  119.                 inc     cx
  120.                 push    dx
  121.                 or      ax,ax
  122.                 jnz     b2h1
  123.                 or      bx,ax
  124.                 jnz     b2h1
  125.  
  126. b2h2:           mov     ah,02h
  127.                 pop     dx
  128.                 add     dl,"0"
  129.                 cmp     dl,"9"
  130.                 jbe     b2h3
  131.                 add     dl,7
  132. b2h3:           int     21h
  133.                 loop    b2h2
  134.  
  135.                 mov     ah,09h
  136.                 mov     dx,offset crlf
  137.                 int     21h
  138.                 ret
  139. bin2hex         endp
  140.  
  141. ;****************************************************************************
  142. ; FINDCHAR advances SI to the next non-white-space character. On return,
  143. ; carry set indicates EOL was encountered; carry clear indicates it was not.
  144. ;****************************************************************************
  145.  
  146. findchar        proc    near
  147.                 lodsb                           ;Get the next character
  148.                 cmp     al,09h                  ;Loop if tab
  149.                 je      findchar
  150.                 cmp     al,20h                  ;Loop if space
  151.                 je      findchar
  152.                 cmp     al,2Ch                  ;Loop if comma
  153.                 je      findchar
  154.                 dec     si                      ;Point SI to the character
  155.                 cmp     al,0Dh                  ;Exit with carry set if end
  156.                 je      eol                     ;of line is reached
  157.  
  158.                 clc                             ;Clear carry and exit
  159.                 ret
  160.  
  161. eol:            stc                             ;Set carry and exit
  162.                 ret
  163. findchar        endp
  164.  
  165. ;****************************************************************************
  166. ; SCANHELP scans the command line for a /? switch. If the switch is found,
  167. ; carry returns set and SI contains the switch offset. If the switch is not
  168. ; found, carry returns clear.
  169. ;****************************************************************************
  170.  
  171. scanhelp        proc    near
  172.                 push    si                      ;Save SI
  173. scanloop:       lodsb                           ;Get a character
  174.                 cmp     al,0Dh                  ;Exit if end of line
  175.                 je      scan_exit
  176.                 cmp     al,"?"                  ;Loop if not "?"
  177.                 jne     scanloop
  178.                 cmp     byte ptr [si-2],"/"     ;Loop if not "/"
  179.                 jne     scanloop
  180.  
  181.                 add     sp,2                    ;Clear the stack
  182.                 sub     si,2                    ;Adjust SI
  183.                 stc                             ;Set carry and exit
  184.                 ret
  185.  
  186. scan_exit:      pop     si                      ;Restore SI
  187.                 clc                             ;Clear carry and exit
  188.                 ret
  189. scanhelp        endp
  190.  
  191. code            ends
  192.                 end     begin
  193.